#!/usr/bin/perl

if ($#ARGV + 1 != 2) {
	printf("usage[0] >> pid\n");
	printf("usage[1] >> collect second, eg:60\n");
	exit;
}

$output = `ls -llt /usr/local/bin | grep FlameGraph`;
if (length($output) == 0) {
	printf("system not exist FlameGraph, need download");
	system("git clone https://github.com/brendangregg/FlameGraph");
	$cmd = sprintf("mv FlameGraph /usr/local/bin");
	system($cmd)
}

$cmd1 = sprintf("perf record -F 99 -e cpu-clock -p %d -g -- sleep %d ", $ARGV[0], $ARGV[1]);
$cmd2 = sprintf("perf script | /usr/local/bin/FlameGraph/stackcollapse-perf.pl | /usr/local/bin/FlameGraph/flamegraph.pl > %d.svg", $ARGV[0]);
$cmd3 = sprintf("rm -f perf.data");

printf("execute_cmd1:%s\n", $cmd1);
printf("execute_cmd2:%s\n", $cmd2);
printf("execute_cmd3:%s\n", $cmd3);

system($cmd1);
system($cmd2);
system($cmd3);

